39_MCU Programming & System Prototyping
I was having trouble getting test code running on a test PIC16 from the Sr. Design leftovers cabinet. I found a dsPIC30 which ended up working perfectly in the same setup I had been using for the PIC16; so the PIC16 may have been faulty. Previously I had been able to turn on and off an LED using GPIO pins on the dsPIC30, but we still needed to successfully program the MCU to create PWM signals, read in analog signals, communicate through SPI (Serial Peripheral Interface) with the external ADC used for the EMG system, and read in digital signals from the encoders.
After reading through the dsPIC30’s data sheet on its output compare systems, I was able to create a PWM signal using its “continuous pulse” mode which simply output a defined pulse repeatedly (the signal was used to light up an LED; motors will be tested with it later). Figure 1 shows the function used to initialize the MCU’s second output compare module to provide a PWM signal. With a period of 65535 clock pulses (PR2 = 0xFFFF), and an ON time from the first pulse (OC2R = 0x0000) to the 40960 pulse (OC2RS = 0xA000), the signal had a duty cycle of 62.5%. The duty cycle could be varied by simply changing the value in the OC2RS register.
Also, the frequency of the PWM signal could be increased by shortening the period value in the PR2 register. The shorter the period is though, the less resolution the duty cycle has. If the period were decreased all the way down to 2 pulses, the signal could only have duty cycles of 0%, 50%, or 100%.
After comparing MPLab’s XC16 compiler’s header files for the dsPIC30 and the dsPIC33EP (actual PIC used in the device), I found that register names were identical in most cases; so the program for our final dsPIC33 will look very similar to the one being used on the test dsPIC30.
Figure 1: Function in C that initializes a PWM signal on the sPIC30